package com.llp.javaweb.servlet;

import com.google.gson.Gson;
import com.llp.javaweb.pojo.User;
import com.llp.javaweb.service.UserService;
import com.llp.javaweb.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author llp
 * {@code @date} 2022年11月06日20:48
 */
@WebServlet("/user/selectAllUser")
public class selectAllUserServlet extends HttpServlet {
    UserService userService=new UserServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         /*
          获取用户列表List集合，将其转换成json，放到请求域中。
         */
        List<User> users = userService.selectAllUser();
        Gson gson=new Gson();
        String s = gson.toJson(users);
        System.out.println(s);
        req.setAttribute("users",s);
        /*
          转发到用户列表页面
         */
        req.getRequestDispatcher("/WEB-INF/templates/product/products_showusers.html").forward(req,resp);

    }
}
